package com.huawei.appgallery.foundation.ui.framework.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.huawei.appmarket.framework.activity.SecureActivity;
import com.huawei.secure.android.common.intent.SafeBroadcastReceiver;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import o.cgx;
import o.cjo;
import o.dga;
import o.dgc;
import o.dgh;
import o.dgm;
import o.djk;
import o.Logger;
import o.dof;
import o.ContentManager;
import o.eod;
import o.epr;
import o.epv;
import o.eqt;
import o.eqv;
import o.est;
import o.eup;
import o.evr;
import o.ll;

public class AbstractBaseActivity<T extends cjo> extends SecureActivity<T> implements dga {
    private static WeakReference<Activity> ॱˊ;
    private static b ॱˋ = null;
    private eqv ʻॱ = eqt.ॱ();
    ll ʼॱ = ll.ˏ(ContentManager.getInstant().getContext());
    private final BroadcastReceiver ˊॱ = new SafeBroadcastReceiver(this) {
        final /* synthetic */ AbstractBaseActivity ˎ;

        {
            this.ˎ = r1;
        }

        public void onReceiveMsg(Context context, Intent intent) {
            String action = intent.getAction();
            if (AbstractBaseActivity.getUpgradeAction().equals(action) || "com.huawei.systemmanager.action.CHANGE_HOME_COUNTRY".equals(action)) {
                eup.ॱ().ˏ(context);
                est.ˎ();
                this.ˎ.ˋ(true);
                this.ˎ.finish();
            }
        }
    };
    private boolean ˋॱ = false;
    private final BroadcastReceiver ˏॱ = new BroadcastReceiver(this) {
        final /* synthetic */ AbstractBaseActivity ˊ;

        {
            this.ˊ = r1;
        }

        public void onReceive(Context context, Intent intent) {
            est.ˎ();
            this.ˊ.ˋ(true);
            this.ˊ.finish();
        }
    };
    private List<dgc> ͺ = new ArrayList();
    protected Menu ॱᐝ;

    public interface b {
        void ˋ(Activity activity);
    }

    public static Activity ʻॱ() {
        if (ॱˊ == null) {
            return null;
        }
        return (Activity) ॱˊ.get();
    }

    public static void ˎ(Activity activity) {
        if (activity == null) {
            ॱˊ = null;
        } else {
            ॱˊ = new WeakReference(activity);
        }
        if (ॱˋ != null) {
            ॱˋ.ˋ(activity);
        }
    }

    public static void ˏ(b bVar) {
        ॱˋ = bVar;
    }

    private static String getUpgradeAction() {
        return ContentManager.getInstant().getContext().getPackageName() + ".action.SELF_UPGRADE";
    }

    public static void sendUpgrade(Context context) {
        Intent intent = new Intent();
        intent.setAction(getUpgradeAction());
        ll ˏ = ll.ˏ(context);
        if (ˏ != null) {
            ˏ.ˋ(intent);
        }
    }

    public boolean ॱˋ() {
        return this.ˋॱ;
    }

    public void ˋ(boolean z) {
        this.ˋॱ = z;
    }

    public void onCreate(@Nullable Bundle bundle) {
        super.onCreate(bundle);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.LOCALE_CHANGED");
        registerReceiver(this.ˏॱ, intentFilter);
        intentFilter = new IntentFilter();
        intentFilter.addAction(getUpgradeAction());
        intentFilter.addAction("com.huawei.systemmanager.action.CHANGE_HOME_COUNTRY");
        this.ʼॱ.ˏ(this.ˊॱ, intentFilter);
        djk.ˋ().ˎ((Activity) this);
        if (this.ʻॱ != null) {
            this.ʻॱ.ॱ();
        }
        if (bundle != null && this.ʻॱ != null) {
            this.ʻॱ.ॱ(bundle.getBoolean("userInfoState", false));
        }
    }

    @SuppressLint({"NewApi"})
    public void onResume() {
        Logger.ˎ("AbstractBaseActivity", "onResume " + getClass().getName());
        super.onResume();
        cgx.ˋ(this);
        ˎ(this);
        if (this.ʻॱ == null || !this.ʻॱ.ˎ()) {
            Logger.ˎ("UserInfoInspector", "base onResume autoLogin");
            ˊॱ();
        } else {
            Logger.ˎ("UserInfoInspector", "base onResume inspect");
            this.ʻॱ.ˊ(this);
        }
        ʼ();
    }

    public void onPause() {
        super.onPause();
        ˎ(null);
        if (!epv.ᐝ(this)) {
            Logger.ˎ("AbstractBaseActivity", "Glide clearMemory");
            Glide.ˎ((Context) this).ʼ();
        }
        est.ॱ();
        S_();
    }

    protected LinkedHashMap ॱˎ() {
        LinkedHashMap linkedHashMap = new LinkedHashMap(3);
        if (epr.ˎ().ˏ() != null) {
            linkedHashMap.put("third_id", epr.ˎ().ˏ());
        }
        if (!dof.ॱ(ʿ())) {
            linkedHashMap.put("page_id", ʿ());
        }
        linkedHashMap.put("service_type", Integer.valueOf(dgm.ˎ((Activity) this)));
        return linkedHashMap;
    }

    protected String ʿ() {
        return null;
    }

    protected void ʼ() {
        dgh.ˏ(getClass().getCanonicalName(), ॱˎ());
    }

    protected void S_() {
        dgh.ॱ(getClass().getCanonicalName(), ॱˎ());
    }

    public void finish() {
        djk.ˋ().ˊ(this);
        super.finish();
    }

    public void onDestroy() {
        super.onDestroy();
        djk.ˋ().ˊ(this);
        unregisterReceiver(this.ˏॱ);
        if (this.ʼॱ != null) {
            this.ʼॱ.ˋ(this.ˊॱ);
        }
        ˋ((Context) this);
        if (this.ʻॱ != null) {
            this.ʻॱ.ˊ();
        }
        this.ͺ.clear();
    }

    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        if (this.ʻॱ != null) {
            bundle.putBoolean("userInfoState", this.ʻॱ.ˎ());
        }
    }

    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        if (keyEvent.getKeyCode() != 82) {
            return super.onKeyDown(i, keyEvent);
        }
        Logger.ˎ("AbstractBaseActivity", getClass().getSimpleName() + ", event.getKeyCode() == KeyEvent.KEYCODE_MENU");
        return true;
    }

    protected void ˊॱ() {
        eod.ˊ((Context) this);
    }

    private void ˋ(Context context) {
        if (context != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService("input_method");
            if (inputMethodManager != null) {
                for (String declaredField : new String[]{"mCurRootView", "mServedView", "mNextServedView"}) {
                    try {
                        Field declaredField2 = inputMethodManager.getClass().getDeclaredField(declaredField);
                        if (!declaredField2.isAccessible()) {
                            declaredField2.setAccessible(true);
                        }
                        Object obj = declaredField2.get(inputMethodManager);
                        if (obj != null && (obj instanceof View)) {
                            if (((View) obj).getContext() == context) {
                                declaredField2.set(inputMethodManager, null);
                            } else {
                                return;
                            }
                        }
                    } catch (IllegalAccessException e) {
                        Logger.ˋ("AbstractBaseActivity", "fixInputMethodManagerLeak IllegalAccessException");
                    } catch (IllegalArgumentException e2) {
                        Logger.ˋ("AbstractBaseActivity", "fixInputMethodManagerLeak IllegalArgumentException");
                    } catch (NoSuchFieldException e3) {
                        Logger.ˋ("AbstractBaseActivity", "fixInputMethodManagerLeak NoSuchFieldException");
                    }
                }
            }
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        boolean onCreateOptionsMenu = super.onCreateOptionsMenu(menu);
        this.ॱᐝ = menu;
        for (dgc ˏ : this.ͺ) {
            ˏ.ˏ(this.ॱᐝ);
        }
        this.ͺ.clear();
        return onCreateOptionsMenu;
    }

    public Menu ʽॱ() {
        return this.ॱᐝ;
    }

    public void ˊ(dgc o_dgc) {
        this.ͺ.add(o_dgc);
    }

    public void onBackPressed() {
        if (!evr.ˊ().ʽ()) {
            super.onBackPressed();
        }
    }
}
